#include	<stdio.h>
#include	<stdlib.h>
#include    <conio.h>
#include	<math.h>
#include	<string.h>
#include	"Uigp/igp.h"
#include	"ImageType/ImageType.h"
#include	"ImageType/ImageTool.h"
#include	"Ucamera/Pt2dType.h"
#include	"InterpolaionLib/Interpolation.h"
#include	"InterpolaionLib/Vornoi/VrLine.h"
#include    "InterpolaionLib/Vornoi/VrCell.h"


static void	read_command_line(int argc, char *argv[],
				  int *width, int *height,
				  char *imageFile, char *outFile );

int menu(void);



int main( int argc, char **argv )
{
	char inFile[256], outFile[256],	file[256];
	image_type	*im,	*tim;
	int	width,	height;
	pt2dA_type	 *apt;
	vrLine_type  *vl = new vrLine_type();;
	vrCell_type  *vc = new vrCell_type();

	int i=0;
	//read command line arguments (Project -> properties.) 
	read_command_line( argc, argv, &width, &height, inFile, outFile );

	if( pt2dA_read_from_file( &apt, inFile)< 0 )
		gpError( "interpolationCA", "Reading  %s  failed" );

	/////////////
	while(1)
	{
		/*get selection and execute the relevant statement*/
		switch(menu())
		{
		case 1:
			{
				im = imageF_interpolation_1( width, height, apt, NULL );
				gpFilename_force_extension( outFile, ".bmp", file );
				tim = imageF_to_image1_S( im, NULL );
				image_write_bmp( tim, file );
				image_destroy( im, 1 );
				image_destroy( tim, 1 );
				break;
			}
		case 2:
			{ 
				vrCell_create(apt,0,vc);

				pt2dA_write_to_file( vrCell_to_apt( vc ), "Before_Filter.txt" );
				
				vrCell_filter( vc);

				pt2dA_write_to_file( vrCell_to_apt( vc ), "After_Filter.txt" );

				break;
			}
		case 3:
			{
				puts("You are quitting\n"); 
				exit(0);
				break;
			}
		default:
			{
				puts("Invalid menu choice\n");
				break;
			}
		}
	}



	if(vl)
	{
		delete vl;
		vl = NULL;
	}
	if(vc)
	{
		delete vc;
		vc = NULL;
	}
	return( 1 );
}

static char *Usage = 
	"Usage: ImagePyramid    [-W#]  [-H#]      imageFile  outFile";

static void	read_command_line(int argc, char *argv[],
				  int *width, int *height,
				  char *inFile, char *outFile )
{
	int     k=1;
	*width = 512;
	if (k < argc && gp_strnicmp(argv[k], "-W", 2) == 0  )
	{
		sscanf( argv[k]+2, "%d", width );
		k++;
	}

	*height = *width;
	if (k < argc && gp_strnicmp(argv[k], "-H", 2) == 0  )
	{
		sscanf( argv[k]+2, "%d", height );
		k++;
	}
	if (k < argc) 
	{
		sprintf( inFile, "%s", argv[k]);
		k++;
	}
	else	error("read_command_line", Usage);
	
	if (k < argc) 
	{
		sprintf( outFile, "%s", argv[k]);
		k++;
	}
	else	error("read_command_line", Usage);
}




/*menu function*/
int menu(void)
{
int reply;
/*display menu options*/
printf("Press 1 to Inverst distance interpolation");
printf("\nPress 2 to create vector");
printf("\nPress 3 to Exit\n\n");

/*scan for user entry*/
scanf("%d", &reply);

return reply;
}
